package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TActiReward;
import com.tencent.wxcloudrun.dto.ActiRewardReqDTO;
import com.tencent.wxcloudrun.mapper.TActiRewardMapper;
import com.tencent.wxcloudrun.service.TActiRewardService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_acti_reward(活动奖励表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TActiRewardServiceImpl extends ServiceImpl<TActiRewardMapper, TActiReward> implements TActiRewardService {

    @Override
    public void add(Long userId, Long actiId, List<ActiRewardReqDTO> rewardList) {
        if (CollectionUtils.isEmpty(rewardList)) {
            return;
        }
        List<TActiReward> dbList = new ArrayList<>(rewardList.size());
        for (ActiRewardReqDTO actiRewardReqDTO : rewardList) {
            TActiReward actiReward = new TActiReward();
            actiReward.setActiId(actiId);
            actiReward.setRewardType(actiRewardReqDTO.getRewardType());
            actiReward.setActiRewardLevel(actiRewardReqDTO.getActiRewardLevel());
            actiReward.setActiRewardType(actiRewardReqDTO.getActiRewardType());
            actiReward.setActiRewardKey(actiRewardReqDTO.getActiRewardKey());
            actiReward.setActiRewardCount(actiRewardReqDTO.getActiRewardCount());
            actiReward.setCreatedTime(LocalDateTime.now());
            actiReward.setUpdatedTime(LocalDateTime.now());
            actiReward.setCreatedBy(String.valueOf(userId));
            actiReward.setUpdatedBy(String.valueOf(userId));
            dbList.add(actiReward);
        }
        this.saveBatch(dbList);
    }

    @Override
    public void delete(Long userId, Long actiId) {
        LambdaUpdateWrapper<TActiReward> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TActiReward::getDeleteFlag, 1);
        wrapper.set(TActiReward::getEnableFlag, 0);
        wrapper.set(TActiReward::getUpdatedTime, LocalDateTime.now());
        wrapper.set(TActiReward::getUpdatedBy, userId);
        wrapper.eq(TActiReward::getId, actiId);
        this.update(wrapper);
    }

    @Override
    public void deleteByActiId(Long actiId) {
        LambdaQueryWrapper<TActiReward> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TActiReward::getActiId, actiId);
        queryWrapper.eq(TActiReward::getEnableFlag, 1);
        this.remove(queryWrapper);
    }

    @Override
    public List<TActiReward> listByActiId(Long actiId) {
        LambdaQueryWrapper<TActiReward> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TActiReward::getActiId, actiId);
        queryWrapper.eq(TActiReward::getEnableFlag, 1);
        return this.list(queryWrapper);
    }
}
